package vertx;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.vertx.java.core.AsyncResult;
import org.vertx.java.core.AsyncResultHandler;
import org.vertx.java.core.Future;
import org.vertx.java.core.Handler;
import org.vertx.java.core.MultiMap;
import org.vertx.java.core.VoidHandler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerFileUpload;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.RouteMatcher;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.platform.Verticle;

public class BridgeServerHttp extends Verticle {
	Logger logger = null;
	Route route = null;

    public void start(final Future<Void> startedResult) {
		logger = container.logger();
		
//		container.deployVerticle("vertx/WebClient.js");
		
//		route = new Route(vertx, eb, logger);

		RouteMatcher rm = new RouteMatcher();
		rm.allWithRegEx("\\/js\\/.+", new Handler<HttpServerRequest>() {
			public void handle(HttpServerRequest req) {
				String staticPath = container.config().getString("static_files", System.getProperty("user.dir"));
				logger.info("static path files " + staticPath);
				req.response().sendFile("./webroot" + req.path());
			}
		});
		rm.allWithRegEx("\\/css\\/.+", new Handler<HttpServerRequest>() {
			public void handle(HttpServerRequest req) {
				String staticPath = container.config().getString("static_files", System.getProperty("user.dir"));
				logger.info("static path files " + staticPath);
				req.response().sendFile("./webroot" + req.path());
			}
		});
		rm.allWithRegEx("\\/img\\/.+", new Handler<HttpServerRequest>() {
			public void handle(HttpServerRequest req) {
				String staticPath = container.config().getString("static_files", System.getProperty("user.dir"));
				logger.info("static path files " + staticPath);
				req.response().sendFile("./webroot" + req.path());
			}
		});
		rm.allWithRegEx("\\/([^\\/]+)", new Handler<HttpServerRequest>() {
			public void handle(HttpServerRequest req) {
				logger.info("Controller: " + req.path());
				req.response().sendFile("./webroot" + req.path());
			}
		});
		rm.allWithRegEx("\\/([^\\/]+)\\/([^\\/]+)", new Handler<HttpServerRequest>() {
			public void handle(HttpServerRequest req) {
				logger.info("Controller: " + req.path());
				req.response().sendFile("./webroot" + req.path());
			}
		});
		rm.allWithRegEx("\\/([^\\/]+)\\/([^\\/]+)\\/([^\\/]+)", new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest req) {
				logger.info("Controller: " + req.path());
            	req.response().sendFile("./webroot" + req.path());
            }
		});
		rm.noMatch(new Handler<HttpServerRequest>() {
		    public void handle(HttpServerRequest req) {
            	req.response().sendFile("./view/404.html");
		    }
		});		
//		rm.get("/vertxbus.js", new Handler<HttpServerRequest>() {
//			public void handle(HttpServerRequest req) {
//				req.response().sendFile("./webroot/js/vertxbus-2.1.js");
//			}
//		});
//		rm.get("/testgetusermedia.html", new Handler<HttpServerRequest>() {
//			public void handle(HttpServerRequest req) {
//				req.response().sendFile("./webroot/js/recorderjs/getusermedia.html");
//			}
//		});
//		rm.get("/example_simple_exportwav.html", new Handler<HttpServerRequest>() {
//			public void handle(HttpServerRequest req) {
//				req.response().sendFile("./webroot/js/recorderjs/example_simple_exportwav.html");
//			}
//		});
//		rm.get("/recorder.js", new Handler<HttpServerRequest>() {
//			public void handle(HttpServerRequest req) {
//				req.response().sendFile("./webroot/js/recorderjs/recorder.js");
//			}
//		});
//		rm.get("/recorderWorker.js", new Handler<HttpServerRequest>() {
//			public void handle(HttpServerRequest req) {
//				req.response().sendFile("./webroot/js/recorderjs/recorderWorker.js");
//			}
//		});
		
		
//	    Vertx vertx = VertxFactory.newVertx();
		HttpServer server = vertx.createHttpServer();
		server.requestHandler(rm);
		// Initialize the SockJS bridge
//		vertx.createSockJSServer(server).bridge(new JsonObject("{prefix: '/eventbus'}"), new JsonArray("{address: 'webclient_address'}"), new JsonArray("{}"));
		vertx.createSockJSServer(server).bridge(new JsonObject().putString("prefix", "/eventbus"), new JsonArray().add(new JsonObject().putValue("address", "webclient_address")), new JsonArray());
		server.listen(8080,"localhost");
    	logger.info(this.getClass().getName() + " started!");
        
    }
 
    
    
    public static void main(String[] args) throws Exception {
    	new BridgeServerHttp().start();
    }
   
}